package com.controller.sys;


import cn.hutool.core.util.IdUtil;
import cn.hutool.core.util.StrUtil;
import com.common.base.BaseController;
import com.entity.sys.SysDept;
import com.service.sys.SysDeptService;
import fuhua.com.anno.CheckPermission;
import fuhua.com.anno.Log;
import fuhua.com.util.ResultUtil;
import fuhua.com.util.TreeUtil;
import org.springframework.web.bind.annotation.*;

import java.util.HashMap;
import java.util.List;
import java.util.Map;

/**
 * 部门
 */
@RestController
@RequestMapping("/sys/dept")
public class SysDeptController extends BaseController<SysDeptService,SysDept> {

    /**
     * 部门树列表
     */
    @GetMapping("/list")
    @CheckPermission(perm = "system:dept:view")
    public ResultUtil list(@RequestParam Map<String,String> map) {
        List<SysDept> list = service.list(map);
        return ResultUtil.success(TreeUtil.buildTreeIsRoot(list,"ROOT"));
    }

    /**
     * 根据id获取信息
     */
    @GetMapping("/getById/{id}")
    public ResultUtil getById(@PathVariable("id") String id) {
        return ResultUtil.success(service.getById(id));
    }

    /**
     * 加载对应角色部门列表树
     */
    @GetMapping(value = "/roleDeptTree/{roleId}")
    public ResultUtil roleDeptTree(@PathVariable("roleId") String roleId) {
        Map<String,String> params = new HashMap<>();
        List<SysDept> deptList = service.list(params);
        Map<String,Object> map = new HashMap<>();
        map.put("deptList", TreeUtil.buildTreeIsRoot(deptList,"ROOT"));
        params.put("id",roleId);
        map.put("checkedKeys", service.selectIdListByRoleId(params));
        return ResultUtil.success(map);
    }

    /**
     * 新增
     */
    @Log(title = "新增部门")
    @PostMapping("/insert")
    @CheckPermission(perm = "system:dept:add")
    public ResultUtil insert(@RequestBody SysDept dept) {
        if (service.checkDeptName(dept)) {
            return ResultUtil.error("名称已存在");
        }
        dept.setId(IdUtil.getSnowflakeNextIdStr());
        service.save(dept);
        return ResultUtil.success();
    }

    /**
     * 修改
     */
    @Log(title = "修改部门")
    @PostMapping("/update")
    @CheckPermission(perm = "system:dept:update")
    public ResultUtil update(@RequestBody SysDept dept) {
        if (service.checkDeptName(dept)) {
            return ResultUtil.error("名称已存在");
        }
        if (StrUtil.isNotBlank(dept.getId()) && dept.getParentId().equals(dept.getId())){
            return ResultUtil.error("修改失败，上级不能是自己");
        }
        service.updateById(dept);
        return ResultUtil.success();
    }

    /**
     * 删除
     */
    @Log(title = "删除部门")
    @DeleteMapping("/delete/{id}")
    @CheckPermission(perm = "system:dept:delete")
    public ResultUtil delete(@PathVariable("id") String id) {
        if (service.hasChildByDeptId(id)) {
            return ResultUtil.error("存在下级,不允许删除");
        }
        if (service.checkDeptExistUser(id)) {
            return ResultUtil.error("该组织存在用户,不允许删除");
        }
        if (service.checkDeptExistRole(id)) {
            return ResultUtil.error("该组织已分配了角色,不允许删除");
        }
        service.removeById(id);
        return ResultUtil.success();
    }
}
